package edu.uoc.tdp.pac4.client.pantalles;

import javax.swing.JPanel;

import edu.uoc.tdp.pac4.common.beans.Perfil;
import edu.uoc.tdp.pac4.common.beans.S1Usuari;
import edu.uoc.tdp.pac4.common.beans.S4InfEmpleats;
import edu.uoc.tdp.pac4.common.exception.ExcepcioConnexioBD;
import edu.uoc.tdp.pac4.common.exception.S4EstadistiquesException;
import edu.uoc.tdp.pac4.common.interfaces.S4InterfaceEstadistiques;
import edu.uoc.tdp.pac4.common.utils.TDSLanguageUtils;

import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterException;
import java.rmi.RemoteException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTable.PrintMode;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class S4InformeEmpleats extends JPanel {

	private static final long serialVersionUID = 1L;

	private S1Usuari _usuariConnectat;
	private S4InterfaceEstadistiques _iEstadistiques;
	private JTable table;
	private JTextPane txtnif;
	private JComboBox cmbperfil;
	private JTextPane txtnom;
	
	/**
	 * Create the panel.
	 */
	public S4InformeEmpleats(S4InterfaceEstadistiques iEstadistiques, S1Usuari usuariConnectat) {
		
		
		_usuariConnectat=usuariConnectat;
		_iEstadistiques=iEstadistiques;
		IniciaComponents();
	}
	
	private void CercarEmpleats(){

		List<S4InfEmpleats> llistaEmpleats = new ArrayList<S4InfEmpleats>();
		
		try {
			String perf="";
			
			if (!cmbperfil.getSelectedItem().toString().equals(TDSLanguageUtils.getMessage("LBL_EST_TOTS"))){perf=cmbperfil.getSelectedItem().toString();}
			llistaEmpleats=_iEstadistiques.CercarEmpleats(_usuariConnectat.getTaller(), txtnif.getText(), txtnom.getText(), perf);
			
			DefaultTableModel modelo=(DefaultTableModel) table.getModel();
			table.removeAll();
			Integer i;
			Integer num=modelo.getRowCount();
			for(i=num-1;i>=0;i--){
				modelo.removeRow(i);
			}
			
			if (llistaEmpleats.size()>0 ){
				Integer filas=0;

				for (S4InfEmpleats item : llistaEmpleats){
															
					modelo.addRow(new Object[filas]);
					table.setValueAt(item.Getdni(),filas , 0);
					table.setValueAt(item.Getnom(), filas, 1);
					table.setValueAt(item.Getcognoms(), filas, 2);
					table.setValueAt(item.Gettaller(), filas, 3);
					table.setValueAt(item.Getperfil(), filas, 4);
					table.setValueAt(item.GetnumReparacions().toString(), filas, 5);
					table.setValueAt(item.GetavgDiasReparacio().toString(), filas, 6);
					filas++;
	            }
				table.updateUI();
			}else{
				modelo.addRow(new Object[]{null, null, null, null, null, null, null, null,null,null,null});
				JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("ERR_EST_NODADES"), "Info", JOptionPane.INFORMATION_MESSAGE);
			}
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("ERR_RMI"), "Error", JOptionPane.ERROR_MESSAGE);
		} catch (S4EstadistiquesException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		} catch (ExcepcioConnexioBD e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void PrintReport(){
		MessageFormat header=new MessageFormat(TDSLanguageUtils.getMessage("LBL_EST_INFEMP"));
		MessageFormat footer=new MessageFormat("- {0} -");
		try {
			table.print(PrintMode.FIT_WIDTH,header,footer);
		} catch (PrinterException e1) {
			JOptionPane.showMessageDialog(this, TDSLanguageUtils.getMessage("ERR_PRINT"), "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	private void IniciaComponents(){
		JLabel lblInformeDempleats = new JLabel(TDSLanguageUtils.getMessage("LBL_EST_INFEMP"));
		lblInformeDempleats.setFont(new Font("Tahoma", Font.BOLD, 17));
		
		JLabel lblPerfil = new JLabel(TDSLanguageUtils.getMessage("LBL_EST_PERFIL"));
		
		JButton button = new JButton(TDSLanguageUtils.getMessage("LBL_EST_CERCAR"));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CercarEmpleats();
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton button_1 = new JButton(TDSLanguageUtils.getMessage("LBL_EST_IMPRIMIR"));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PrintReport();
			}
		});
		
		cmbperfil = new JComboBox();
		cmbperfil.setModel(new DefaultComboBoxModel(new String[] {TDSLanguageUtils.getMessage("LBL_EST_TOTS"),Perfil.ADMINISTRATIU.toString(),"CAP TALLER",Perfil.MECANIC.toString()}));
		
		JLabel label = new JLabel("NIF");
		
		txtnif = new JTextPane();
		
		JLabel lblNom = new JLabel(TDSLanguageUtils.getMessage("LBL_EST_NOM"));
		
		txtnom = new JTextPane();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 827, Short.MAX_VALUE)
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
							.addGap(10)
							.addComponent(txtnif, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
							.addGap(26)
							.addComponent(lblNom, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtnom, GroupLayout.PREFERRED_SIZE, 184, GroupLayout.PREFERRED_SIZE)
							.addGap(64)
							.addComponent(lblPerfil, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(cmbperfil, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 101, Short.MAX_VALUE)
							.addComponent(button, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(button_1, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
							.addContainerGap(739, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblInformeDempleats)
							.addContainerGap(668, Short.MAX_VALUE))))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(8)
					.addComponent(lblInformeDempleats)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(txtnom, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNom)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblPerfil)
							.addComponent(cmbperfil, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(button))
						.addComponent(txtnif, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label))
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(button_1)
					.addContainerGap())
		);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null},
			},
			new String[] {
				"DNI", TDSLanguageUtils.getMessage("LBL_EST_NOM"), TDSLanguageUtils.getMessage("LBL_EST_TALLER"), TDSLanguageUtils.getMessage("LBL_EST_COGNOMS"),  TDSLanguageUtils.getMessage("LBL_EST_PERFIL"), TDSLanguageUtils.getMessage("LBL_EST_REPS"), TDSLanguageUtils.getMessage("LBL_EST_AVGREPS")
			}
		));
		table.getColumnModel().getColumn(1).setPreferredWidth(100);
		table.getColumnModel().getColumn(2).setPreferredWidth(200);
		table.getColumnModel().getColumn(3).setPreferredWidth(160);
		table.getColumnModel().getColumn(4).setPreferredWidth(120);
		table.getColumnModel().getColumn(5).setPreferredWidth(100);
		table.getColumnModel().getColumn(6).setPreferredWidth(130);
		table.setFont(new Font("Tahoma", Font.PLAIN, 10));
		scrollPane.setViewportView(table);
		setLayout(groupLayout);
	}
}
